package com.hcbd.tdwt.ui.xingyu.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcbd.tdwt.R;


public class CustomDialog extends Dialog implements View.OnClickListener{


    private EditText editContent;
    private TextView titleTV;
    private ImageView shutDown;
    private OnCloseListener listener;
    public Button okBtn;
    public Button quiteBtn;

    private String title;
    public int dialogType;
    public static final int ADD_DIALOG=1;
    public static final int DEL_DIALOG=2;
    private Drawable okBtnBg;
    private Drawable quiteBtnBg;
    private int okTvColor;
    private int quiteTvColor;
    public CustomDialog(Context context, String title, int dialogType, OnCloseListener listener) {
        super(context);
        this.listener=listener;
        this.title=title;
        this.dialogType=dialogType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);
        setCanceledOnTouchOutside(false);
        init();
    }
    public void init(){
        editContent=findViewById(R.id.edit_content);
        titleTV=findViewById(R.id.title);
        titleTV.setText(title);
        shutDown=findViewById(R.id.shutDown);
        okBtn=findViewById(R.id.okBtn);
        quiteBtn=findViewById(R.id.quiteBtn);
        shutDown.setOnClickListener(this);
        okBtn.setOnClickListener(this);
        quiteBtn.setOnClickListener(this);
        if(okBtnBg!=null){
            okBtn.setTextColor(okTvColor);
            okBtn.setBackground(okBtnBg);
        }
        if(quiteBtnBg!=null){
            quiteBtn.setBackground(quiteBtnBg);
            quiteBtn.setTextColor(quiteTvColor);
        }
        if(dialogType==ADD_DIALOG){
            editContent.setVisibility(View.VISIBLE);
        }else if(dialogType==DEL_DIALOG){
            editContent.setVisibility(View.GONE);
        }
    }




    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.okBtn:
                if(listener != null){
                    listener.onOk(this,editContent.getText().toString().trim(),v);
                }
                editContent.setText("");
                break;
            case R.id.quiteBtn:
                this.dismiss();
            case R.id.shutDown:
                this.dismiss();
                break;
        }
    }

    public void setOkBtnStyle(Drawable okBtnBg,int color) {
        this.okBtnBg = okBtnBg;
        this.okTvColor=color;
    }

    public void setQuiteBtnStyle(Drawable quiteBtnBg,int color) {
        this.quiteBtnBg = quiteBtnBg;
        this.quiteTvColor=color;
    }



    public interface OnCloseListener{
        void onOk(Dialog dialog, String content,View view);
    }


    public static  void show(Dialog dialog){
        if(dialog!=null&&!dialog.isShowing()){
            dialog.show();
        }
    }
    public static  void shutDown(Dialog dialog){
        if(dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }
}
